﻿using System;
using System.IO;
using System.Collections.Generic;
using DevExpress.Web.ASPxCallback;

using YhmOA.Model;
using YhmOA.Common;
using YhmOA.BLL;
using YhmOA.Common.Extensions;

namespace YhmOA.Integrated.Car
{
    public partial class ViolatedRecordForm : FormPage<Integrated_Car_ViolatedRecord>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bool isReadOnly = Mode == FormMode.ReadOnly;
                //绑定控件的数据源
                if (!isReadOnly)
                {
                    //绑定车牌号码
                    List<string> plateNumList = Integrated_Car_CarBLL.GetPlateNumList();
                    this.txt_PlateNum.Items.AddRange(plateNumList);
                    //绑定司机姓名
                    List<string> driverNameList = Integrated_Car_DriverBLL.GetNameList();
                    this.txt_Driver.Items.AddRange(driverNameList);
                    //绑定处罚部门
                    List<Integrated_Car_Dic> penalizeDepartList = Integrated_Car_DicBLL.GetPenalizeDepartList();
                    foreach (var item in penalizeDepartList)
                    {
                        this.txt_PenalizeDepart.Items.Add(item.Value);
                    }
                    //绑定违章代码
                    this.txt_ViolatedCode.DataSource = Integrated_Car_DicBLL.GetViolatedCodeList();
                    this.txt_ViolatedCode.DataBind();
                }
                this.btnCancel.Visible = !isReadOnly;
                this.btnSave.Visible = !isReadOnly;
                base.LoadData(isReadOnly);
            }
        }

        #region Private Method
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns>保存是否成功</returns>
        private bool save(bool isFY)
        {
            bool isSucceed = false;
            Local = CreateInstance();

            switch (Mode)
            {
                case FormMode.ReadOnly:
                    break;
                case FormMode.Add:
                    Local.InsertDate = DateTime.Now;
                    //记录费用
                    if (isFY)
                    {
                        string sn = string.Empty;
                        Integrated_Car_CostRecordBLL.AddCostRecord(Local.PlateNum, Local.PenalizeMoney, CostType.Violated, string.Empty, string.Empty, out sn);
                    }
                    Local.SN = Local.Driver.ToShortPY() + Local.InsertDate.ToString("yyyyMMddHHmmss");
                    isSucceed = Dal.Add(Local);
                    break;
                case FormMode.Modify:
                    Local.UpdateDate = DateTime.Now;
                    isSucceed = Dal.Update(Local, this._ID);
                    break;
                default:
                    break;
            }

            return isSucceed;
        }

        protected void cbSave_Callback(object source, CallbackEventArgs e)
        {
            ASPxCallback callbackControl = source as ASPxCallback;
            bool isSucceed = this.save(bool.Parse(e.Parameter));
            callbackControl.JSProperties["cpIsSucceed"] = isSucceed;
        }
        #endregion
    }
}